#include <stdio.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/sem.h>

int main(void)
{
	key_t key;
	int semid;
	int nsems;
	int proj_id;
	
	key=IPC_PRIVATE;
	nsems=2;
	semid=semget(key,nsems,0666);
	if(semid==-1)
		perror("cannot create semaphore set");
	else
		printf("1. key=IPC_PRIVATE,  semid= %d\n",semid);

	proj_id=1;
	key=ftok("/home/program",proj_id);
	if(key==-1)
		perror("cannot generate IPC key");

	semid=semget(key,nsems,IPC_CREAT|0666);
        if(semid==-1)
                perror("cannot create semphore set");
	else
        	printf("2. key=%d generated by ftok, semphore set semid= %d\n",key,semid);
	
	semid=semget(key,nsems,IPC_CREAT |IPC_EXCL |0666);
	if(semid==-1)
		perror("cannot create semphore set");

	semid=semget(key,nsems,IPC_CREAT |0666);
	if(semid==-1)
		perror("cannot create semphore set");	
	else
		printf("Access the existing semphore set\n");

	return 0;
}
